package com.bw.home.ui

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.home.R
import com.bw.home.databinding.FragmentRecommendBinding
import com.bw.home.databinding.ItemSimplevideoBinding
import com.bw.home.intent.SimpleRecommendIntent
import com.bw.home.model.entity.SimpleVideoEntity
import com.bw.home.model.service.SimpleRecommendModel
import com.bw.home.state.SimpleRecommendState
import com.bw.imageloader.ImageLoaderManager
import com.bw.mvicore.ui.BaseMVIFragment

/**
 * @author:created by hahajing
 * 时间：2024/4/29 0029
 * 企鹅号:444511958
 * 描述:
 */
class RecommendFragment:BaseMVIFragment() {
    private lateinit var binding: FragmentRecommendBinding
    private lateinit var viewModel:SimpleRecommendModel
    private lateinit var adapter: RecommendAdapter

    private var page:Int = 0
    private val pagesize:Int=20
    override fun generateModel() {
        viewModel= ViewModelProviders.of(this).get(SimpleRecommendModel::class.java)
    }

    override fun initEvent() {

    }

    override fun initView() {
        super.initView()
        binding.rvHomeDetailDescriptionRecommend.layoutManager = GridLayoutManager(context,2)

        adapter=RecommendAdapter()
        binding.rvHomeDetailDescriptionRecommend.adapter=adapter
    }

    override fun generateFragmentRootView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentRecommendBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun lazyLoad() {
        generateLifeCycleLaunch {
            viewModel.intents.send(SimpleRecommendIntent.GetSimpleRecommendIntent(page,pagesize))
        }
    }

    override fun handleState() {
        generateLifeCycleLaunch {
            viewModel.state.collect{
                when(it){
                    is SimpleRecommendState.SimpleRecommendDefaultState->
                    {}
                    is SimpleRecommendState.SimpleRecommendSuccessState->{
                        adapter.append(it.datas)
                    }
                    is SimpleRecommendState.SimpleRecommendFailedState->{
                        showMsg("获取推荐列表数据失败，info:${it.throwable.message}")
                    }
                }
            }
        }
    }

    inner class RecommendAdapter: RecyclerView.Adapter<RecommendAdapter.ViewHolder>(){
        private lateinit var mContext: Context
        inner class ViewHolder(view:View): RecyclerView.ViewHolder(view){
            val binding: ItemSimplevideoBinding by lazy {
                ItemSimplevideoBinding.bind(view)
            }
        }

        private var list:MutableList<SimpleVideoEntity> = mutableListOf()

        /**
         * 追加数据
         */
        fun append(_list:List<SimpleVideoEntity>){
            this.list.addAll(_list)
            this.notifyDataSetChanged()
        }

        /**
         * 刷新数据
         */
        fun refresh(_list:MutableList<SimpleVideoEntity>){
            this.list.clear()
            this.list.addAll(_list)
            this.notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            this.mContext=parent.context
            val view =  LayoutInflater.from(parent.context).inflate(R.layout.item_simplevideo,parent,false)
            return ViewHolder(view)
        }

        override fun getItemCount(): Int {
            return this.list.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val videoData = this.list.get(position)
            holder.binding.tvHomeItemSimplevideoName.text = videoData.title
            holder.binding.tvHomeItemSimplevideoDescription.text = videoData.description
            ImageLoaderManager.getInstance().loadFileIntoImageView(this.mContext,videoData.avatar_url,holder.binding.ivHomeItemSimplevideoAuthorimg,null)
            ImageLoaderManager.getInstance().loadFileIntoImageView(this.mContext,videoData.videomainimag,holder.binding.ivHomeItemSimplevideoVideomainimg,null)

            holder.binding.clItemSimplevideo.tag=videoData
            holder.binding.clItemSimplevideo.setOnClickListener(listener)
        }

        private val listener:View.OnClickListener = object :View.OnClickListener{
            override fun onClick(v: View?) {
                val bundle = Bundle()

                v?.let {
                    val videoData = it.tag as SimpleVideoEntity
                    bundle.putParcelable("entity",videoData)
                    getBaseActivity().route(DetailActivity::class.java,bundle)
                }

            }

        }
    }
}